package com.idbk.chargestation.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;

import java.util.List;

/**
 * 扫码
 * @author zhx
 *
 */

public class JsonQrScan extends JsonBase implements Parcelable {
	
	public final static String KEY = "JsonQrScan";
	/**
	 * 扫码桩标志位
	 */
	public final static int CHOOSE_PILE = 1;
	/**
	 * 预约桩标志位
	 */
	public final static int BOOK_PILE = 0;
	
	@SerializedName("id")
	public int id;//充电桩id
	
	@SerializedName("pileSn")
	public String pileSn;//充电桩序列号
	
	@SerializedName("address")
	public String address;//地址
	
	@SerializedName("stationId")
	public int stationId;//充电站的Id
		
	@SerializedName("pointName")
	public String pointName;//充电点名称
		
	public List<String> imageURLs;//充电站的Id
		
	@SerializedName("tariff")
	public JsonPileTariff tariff;
		
	@SerializedName("pileType")
	public int pileType;//电桩类型
	
	@SerializedName("pileStatue")
	public int pileStatue;//电桩状态
	
	@SerializedName("gunA")
	public int gunA;//充电枪A
	
	@SerializedName("gunB")
	public int gunB;//充电枪B
	
	@SerializedName("isConnect")
	public boolean isConnect;//判断是否连接成功
	
	@SerializedName("performance")
	public String performance;//性能描述

	@SerializedName("bespeakMinutes")
	public int bespeakMinutes;//最大可预约时间, 单位分钟

	@SerializedName("compares")
	public List<JsonQrScan> compares;//扫码与预约结果比较

	@SerializedName("bookSupport")
	public boolean bookSupport;//是否支持预约 1：表示支持；0：表示不支持

	@SerializedName("bookMoney")
	public double bookMoney;//预约单价

	@SerializedName("balance")
	public double balance;//余额

	@SerializedName("perMinutes")
	public int perMinutes;//预约费按时间收费

	@SerializedName("priceTypeEnum")
	public int priceTypeEnum;//电价类型。1：通用电价，2：分时电价

	@SerializedName("isBookedOther")
	public boolean isBookedOther;//是否被他人预约占用

	@SerializedName("chargeSpeed")
	public String chargeSpeed;//充电速率的描述

	@SerializedName("chargeGun")
	public String chargeGun;//充电枪的选择

	@SerializedName("chargeMethod")
	public String chargeMethod;//选择充电的方式

	@SerializedName("chargeLimit")
	public String chargeLimit;//数额

	@SerializedName("groundLockNum")
	public int groundLockNum;//地锁数量

	@SerializedName("lock1")
	public int lock1;//地锁1

	@SerializedName("lock2")
	public int lock2;//地锁2

	@SerializedName("isUnLocked")
	public boolean isUnLocked;

	@SerializedName("gunInfos")
	public List<JsonGunInfor> gunInfos;//充电枪属性

	public JsonQrScan(){

	}

	/**
	 * 获取 电价类型 的字符串
	 * @return 当前电价类型的字符串
	 */
	public String getPriceTypeString(){
		if(priceTypeEnum == 1){
			return "通用电价";
		} else if(priceTypeEnum == 2){
			return "分时电价";
		} else {
			return "N/A";
		}
	}

	/**
	 * 获取 资费信息 的字符串
	 * @param flag 桩类型 0：预约桩，1：扫码桩
	 */
	public String tariffInformation(int flag) {
		String tariffInfor;
		String STR_PILE_INFO_1 =
				"序列号：%s<br/>" +
						"类    型：%s<br/>" +
						"预    约：%s<br/>" +
						"收    费：%s<br/>" +
						"费    率：<font color='red'>%s</font>元/度";
		String STR_PILE_INFO_2 =
				"序列号：%s<br/>" +
						"类    型：%s<br/>" +
						"预    约：%s<br/>" +
						"收    费：%s<br/>" +
						"费    率：<br/>";
		if (tariff != null) {
			if (priceTypeEnum == JsonPointPile.PRICE_TYPE_NORMAL) {
				if (flag == CHOOSE_PILE){
					tariffInfor = String.format(STR_PILE_INFO_1, pileSn, performance,
							"未预约", getPriceTypeString(), tariff.getCommonPriceString());
				} else {
					tariffInfor = String.format(STR_PILE_INFO_1, pileSn, performance,
							"预约中", getPriceTypeString(), tariff.getCommonPriceString());
				}
			}else if (priceTypeEnum == JsonPointPile.PRICE_TYPE_TIME) {
				if (flag == CHOOSE_PILE) {
					tariffInfor = String.format(STR_PILE_INFO_2, pileSn, performance,
							"未预约", getPriceTypeString(), tariff.getCommonPriceString());
				} else {
					tariffInfor = String.format(STR_PILE_INFO_2,  pileSn, performance,
							"预约中", getPriceTypeString(), tariff.getCommonPriceString() );
				}

				StringBuffer sb = new StringBuffer(tariffInfor);
				String[] timeSpans = new String[]{
						tariff.time1StartAt,tariff.time2StartAt,tariff.time3StartAt,
						tariff.time4StartAt,tariff.time5StartAt,tariff.time6StartAt,
						tariff.time7StartAt,tariff.time8StartAt,tariff.time9StartAt,
						tariff.time10StartAt,tariff.time11StartAt,tariff.time12StartAt,
				};
				int[] prices = new int[]{
						tariff.time1PriceCode,tariff.time2PriceCode,tariff.time3PriceCode,
						tariff.time4PriceCode,tariff.time5PriceCode,tariff.time6PriceCode,
						tariff.time7PriceCode,tariff.time8PriceCode,tariff.time9PriceCode,
						tariff.time10PriceCode,tariff.time11PriceCode,tariff.time12PriceCode,
				};
				for (int i = 0; i < timeSpans.length; i++) {
					if(timeSpans[0] == null){
						break;
					}
					//最后一个
					if(i == timeSpans.length - 1){
						String start = getSplitTimeString(timeSpans[i]);
						String end = "24:00";
						appendPriceInfo(sb,start,end,prices[i]);
					} else {
						if(timeSpans[i + 1] == null){
							String start = getSplitTimeString(timeSpans[i]);
							String end = "24:00";
							appendPriceInfo(sb,start,end,prices[i]);
							break;
						} else {
							String start = getSplitTimeString(timeSpans[i]);
							String end = getSplitTimeString(timeSpans[i + 1]);
							appendPriceInfo(sb,start,end,prices[i]);
						}
					}
				}
				tariffInfor = sb.toString();
			} else {
				tariffInfor = "非官方电桩不支持显示资费信息";
			}
		} else {
			tariffInfor = "获取数据失败，请重试";
		}
		return tariffInfor;
	}

	private void appendPriceInfo(StringBuffer sb, String start, String end, int priceIndex){
		sb.append("&emsp;&emsp;&emsp;&emsp;");
		sb.append(start);
		sb.append("-");
		sb.append(end);
		sb.append(" <font color='red'>");
		sb.append(tariff.getSplitTimePriceInfo(priceIndex));
		sb.append("</font>元/度<br/>");
	}

	/**
	 * 由于服务器给的 包含年月日的时间字符串，这里需要截取 小时和分 相关的字符
	 * 1970-01-01 00:00:00
	 * @return 小时和分的时间字符串
	 */
	private String getSplitTimeString(String splitTime){
		//截取长度 应该为
		if(splitTime == null || splitTime.length() < 17){
			return "格式错误";
		} else {
			return splitTime.substring(11, 16);
		}
	}

	/**
	 * 是否需要开地锁，包括判断是否有地锁
	 * @return boolean 有地锁且需要开锁返回true 否则为false
	 */
	public boolean isNeedOpenLock() {
		return groundLockNum > 0 && (!isUnLocked)
				&& (lock1 == JsonPointPile.STATUS_LOCK || lock2 == JsonPointPile.STATUS_LOCK);
	}

	/**
	 * 是否为双枪桩
	 */
	public boolean isDoubleGun() {
		return pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_ASYN
				|| pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_SYN
				|| pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_SINGLE_PHASE
				|| pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_THREE_PHASE;
	}

	public boolean isDcCharging() {
		return pileType == JsonPointPile.PILE_TYPE_DC
				|| pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_ASYN
				|| pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_SYN
				|| pileType == JsonPointPile.PILE_TYPE_DC_MULTI_GUN_SYN;
	}

	public boolean isAcCharging() {
		return pileType == JsonPointPile.PILE_TYPE_AC_SINGLE_PHASE
				|| pileType == JsonPointPile.PILE_TYPE_AC_THREE_PHASE
				|| pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_SINGLE_PHASE
				|| pileType == JsonPointPile.PILE_TYPE_AC_DOUBLE_GUN_THREE_PHASE
				|| pileType == JsonPointPile.PILE_TYPE_AC_MULTI_GUN_SINGLE_SYN
				|| pileType == JsonPointPile.PILE_TYPE_AC_MULTI_GUN_THREE_SYN;

	}

	/**
	 * 是否为单枪桩
	 */
	public boolean isSingleGun() {
		return pileType == JsonPointPile.PILE_TYPE_DC
				|| pileType == JsonPointPile.PILE_TYPE_AC_SINGLE_PHASE
				|| pileType == JsonPointPile.PILE_TYPE_AC_THREE_PHASE;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(this.id);
		dest.writeString(this.pileSn);
		dest.writeString(this.address);
		dest.writeInt(this.stationId);
		dest.writeString(this.pointName);
		dest.writeStringList(this.imageURLs);
		dest.writeParcelable(this.tariff, flags);
		dest.writeInt(this.pileType);
		dest.writeInt(this.pileStatue);
		dest.writeInt(this.gunA);
		dest.writeInt(this.gunB);
		dest.writeByte(this.isConnect ? (byte) 1 : (byte) 0);
		dest.writeString(this.performance);
		dest.writeInt(this.bespeakMinutes);
		dest.writeTypedList(this.compares);
		dest.writeByte(this.bookSupport ? (byte) 1 : (byte) 0);
		dest.writeDouble(this.bookMoney);
		dest.writeDouble(this.balance);
		dest.writeInt(this.perMinutes);
		dest.writeInt(this.priceTypeEnum);
		dest.writeByte(this.isBookedOther ? (byte) 1 : (byte) 0);
		dest.writeString(this.chargeSpeed);
		dest.writeString(this.chargeGun);
		dest.writeString(this.chargeMethod);
		dest.writeString(this.chargeLimit);
		dest.writeInt(this.groundLockNum);
		dest.writeInt(this.lock1);
		dest.writeInt(this.lock2);
		dest.writeByte(this.isUnLocked ? (byte) 1 : (byte) 0);
		dest.writeTypedList(this.gunInfos);
	}

	protected JsonQrScan(Parcel in) {
		this.id = in.readInt();
		this.pileSn = in.readString();
		this.address = in.readString();
		this.stationId = in.readInt();
		this.pointName = in.readString();
		this.imageURLs = in.createStringArrayList();
		this.tariff = in.readParcelable(JsonPileTariff.class.getClassLoader());
		this.pileType = in.readInt();
		this.pileStatue = in.readInt();
		this.gunA = in.readInt();
		this.gunB = in.readInt();
		this.isConnect = in.readByte() != 0;
		this.performance = in.readString();
		this.bespeakMinutes = in.readInt();
		this.compares = in.createTypedArrayList(JsonQrScan.CREATOR);
		this.bookSupport = in.readByte() != 0;
		this.bookMoney = in.readDouble();
		this.balance = in.readDouble();
		this.perMinutes = in.readInt();
		this.priceTypeEnum = in.readInt();
		this.isBookedOther = in.readByte() != 0;
		this.chargeSpeed = in.readString();
		this.chargeGun = in.readString();
		this.chargeMethod = in.readString();
		this.chargeLimit = in.readString();
		this.groundLockNum = in.readInt();
		this.lock1 = in.readInt();
		this.lock2 = in.readInt();
		this.isUnLocked = in.readByte() != 0;
		this.gunInfos = in.createTypedArrayList(JsonGunInfor.CREATOR);
	}

	public static final Creator<JsonQrScan> CREATOR = new Creator<JsonQrScan>() {
		@Override
		public JsonQrScan createFromParcel(Parcel source) {
			return new JsonQrScan(source);
		}

		@Override
		public JsonQrScan[] newArray(int size) {
			return new JsonQrScan[size];
		}
	};
}
